package com.shujing.controller;

import com.shujing.pojo.Result;
import com.shujing.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/4/9
 */
@Slf4j
@RequestMapping
@RestController
public class FileUploadController {

    /**
     * 上传图片
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result<String> upLoad(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        // 保证文件的名字是唯一的，防止文件覆盖
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // 把文件内容存储在本地上
        // file.transferTo(new File("D:\\OneDrive\\桌面\\SpringBoot3+Vue3全套教程\\img\\" + filename));

        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
